package Main;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import java.util.ArrayList;
import java.util.List;


//import com.sun.util.logging.Level;
//import oracle.adfmf.util.Utility;
public class ProjectList {
    private List s_project = null;
    private Project editProject = new Project();
    private boolean newProj = false;
    public ProjectList() {
        if (s_project == null){
            s_project =  new ArrayList();                
        }
        Execute();
    }
    public Project[] getProjects() {
        Project e[] = null;

        e = (Project[])s_project.toArray(new Project[s_project.size()]);

        return e;
    }

    public int getProjectCount() {
        return s_project.size();
    }
   
  
    //lay du an theo id
    public Project getProjectById(int empId) {
       
        Project e = null;
        int count = s_project.size();
        for (int x = 0; x < count; x++) {
            e = (Project)s_project.get(x);
            if (e.getVP_ID() == empId) {
                break;
            }
        }      
        return e;
    }
    ////////////////////execute - insert-update-delete vo database
    public void Execute() {
        try{
            Connection conn =  DBConnection.getConnection();
            s_project.clear();
            conn.setAutoCommit(false);
            PreparedStatement stat = conn.prepareStatement("select * from VP_PROJECT");
            ResultSet rs = stat.executeQuery();
            while (rs.next()) 
            {
                int id = rs.getInt("VP_ID");
                String code =  rs.getString("VP_CODE");
                String name =  rs.getString("VP_NAME");
                String dscr =  rs.getString("VP_DESCRIPTION");
                String enflag =  rs.getString("VP_ENABLE_FLAG");
                Project pro = new Project(id, code,name,dscr, enflag);
                s_project.add(pro);
                
            }
            rs.close();
        }catch (SQLException e) {
            System.err.println(e.getMessage());
        } catch (Exception e) {
            System.err.println(e.getMessage());
        }
    }
    public boolean InsertProject(){
        boolean rs =false;
        try{
        Connection conn =  DBConnection.getConnection();
            conn.setAutoCommit(false);
          //  String SQLInsert = "INSERT INTO VP_PROJECT (VP_ID, VP_CODE, VP_NAME, VP_DESCRIPTION, VP_ENABLE_FLAG) VALUES (" + vp_id + ", " + Code + ", " + Name + "," + Descrip + "," + enable_flag + ")";
          String SQLInsert = "INSERT INTO VP_PROJECT (VP_ID, VP_CODE, VP_NAME, VP_DESCRIPTION, VP_ENABLE_FLAG) VALUES (?,?,?,?,?)";
            PreparedStatement stat = conn.prepareStatement(SQLInsert);
            stat.setInt(1,    editProject.getVP_ID());
            stat.setString(2, editProject.getVP_CODE());
            stat.setString(3, editProject.getVP_NAME());
            stat.setString(4, editProject.getVP_DESCRIPTION());
            stat.setString(5, editProject.getVP_ENABLE_FLAG());
            stat.execute();
            conn.commit();
            rs =true;

        } catch (SQLException e) {
            System.err.println(e.getMessage());
        }
        catch (Exception e) {
            System.err.println(e.getMessage());
        }
        return rs;
    }
    public boolean UpdateProject (Project pro){
        boolean rs = false;
        try{
        Connection conn = DBConnection.getConnection();
            String SQLUpdate = "UPDATE VP_PROJECT SET VP_CODE = ?, VP_NAME = ?, VP_DESCRIPTION = ?, VP_ENABLE_FLAG = ? WHERE VP_ID = ?";
            PreparedStatement stat = conn.prepareStatement(SQLUpdate);            
            stat.setString(1, pro.getVP_CODE());
            stat.setString(2, pro.getVP_NAME());
            stat.setString(3, pro.getVP_DESCRIPTION());
            stat.setString(4, pro.getVP_ENABLE_FLAG());
            stat.setInt(5, pro.getVP_ID());
            stat.execute();
            conn.commit();
            rs =true;
            }catch (SQLException e)
            {
                System.err.print(e.getMessage());
            }
        catch (Exception e)
        {
            System.err.println(e.getMessage());
        }
     return rs;   
    }
    public boolean DeleteProject (int id){
    boolean rs = false ;
    try
    {
        Connection conn = DBConnection.getConnection();
        String SQLDelete = "DELETE VP_PROJECT WHERE VP_ID = ?";
        PreparedStatement stat = conn.prepareStatement(SQLDelete);
        stat.setInt(1,id);
        stat.execute();
        conn.commit();
        rs=true;
    } catch (SQLException e) 
    {
        System.err.println(e.getMessage());
        }
    catch (Exception e){
        System.err.print(e.getMessage());    
    }
    return rs;
    }


}
